﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Configuration;
using System.IO;

namespace GenerateWorkItem
{
    public class GenerateHelpClass
    {
        private string helpClassName;
        private EnterpriseManagementGroup mg;
        private StringBuilder finalClass;
        private List<ManagementPack> allManagementPacks = null;
        private List<ManagementPack> AllManagementPacks
        {
            get
            {
                if (allManagementPacks == null)
                    allManagementPacks = mg.ManagementPacks.GetManagementPacks().ToList<ManagementPack>();
                return allManagementPacks;
            }
        }

        public GenerateHelpClass(EnterpriseManagementGroup mg, string helpClassName)
        {
            this.helpClassName = helpClassName;
            this.mg = mg;
            finalClass = new StringBuilder();
            buildUsingBlock();
            generateEnums();
            writeClass();
        }

        private void buildUsingBlock()
        {
            string temp = "using System;using System.Collections.Generic;using System.Linq;using System.Web;using Microsoft.EnterpriseManagement;using Microsoft.EnterpriseManagement.Common;using Microsoft.EnterpriseManagement.Configuration.IO;using Microsoft.EnterpriseManagement.Configuration;using System.IO;using System.Text;";
            foreach (string s in temp.Split(';'))
            {
                if (s.Length > 0)
                    finalClass.AppendLine(s + ";");
            }
            finalClass.AppendLine("");
            finalClass.AppendLine("namespace " + helpClassName);
            finalClass.AppendLine("{");
        }



        /// <summary>
        /// Generates enums used by all classes in SCSM
        /// </summary>
        private void generateEnums()
        {
            Dictionary<string, List<string>> addedEnums = new Dictionary<string, List<string>>();
            foreach (ManagementPack mp in AllManagementPacks)
            {
                foreach (var enumeration in mp.GetEnumerations())
                {
                    //string enumName = enumeration.Name.Split('.')[0];
                    string[] enumNameArray = enumeration.Name.Split('.');
                    string enumName = "";
                    string delim = "";
                    if (enumNameArray.Length > 1)
                        for (int i = 0; i < enumNameArray.Length - 1; i++)
                        {
                            enumName += delim + enumNameArray[i];
                            delim = "_";
                        }
                    else
                        enumName = enumeration.Name;
                    
                    if (enumName.ToLower() == "enum")
                        continue;
                    if (!addedEnums.ContainsKey(enumName))
                        addedEnums.Add(enumName, new List<string>());

                    //  string enumValue = enumeration.Name.Substring(enumeration.Name.IndexOf('.') + 1).Replace('.','_');
                    string enumValue = enumNameArray[enumNameArray.Length - 1];
                    if (addedEnums[enumName].Contains(enumValue) || enumValue.ToLower() == "enumeration" || enumValue.ToLower().EndsWith("enum"))
                        continue;
                    addedEnums[enumName].Add(enumValue);
                }
            }
            buildEnums(addedEnums);
        }

        private void buildEnums(Dictionary<string, List<string>> addedEnums)
        {
            foreach (string enumName in addedEnums.Keys)
            {
                if (addedEnums[enumName].Count > 0)
                {
                    finalClass.AppendLine("public enum " + enumName);
                    finalClass.AppendLine("{");
                    foreach (string enumValue in addedEnums[enumName])
                    {
                        finalClass.AppendLine(enumValue + ",");
                    }
                    finalClass.AppendLine("}");
                    finalClass.AppendLine("");
                }

            }
        }

        private void writeClass()
        {
            finalClass.AppendLine("}");
            StreamWriter sw = File.CreateText(helpClassName + ".cs");
            string toWrite = finalClass.ToString();
            sw.Write(toWrite);

            sw.Close();

        }



    }
}
